package com.ztao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.common.wemedia.dtos.WmMaterialDto;
import com.heima.model.common.wemedia.pojos.WmMaterial;
import com.heima.utils.thread.WmThreadLocalUtil;
import com.ztao.mapper.WmMaterialMapper;
import com.ztao.service.FileStorageService;
import com.ztao.service.WmMaterialService;
import lombok.SneakyThrows;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class WmMaterialServiceImpl implements WmMaterialService {
    @Resource
    private FileStorageService fileStorageService;
    @Resource
    private WmMaterialMapper wmMaterialMapper;
    @Override
    @SneakyThrows
    public ResponseResult uploadPicture(MultipartFile multipartFile) {
        if(multipartFile==null||multipartFile.getSize()<=0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        String url = fileStorageService.uploadImgFile(multipartFile.getOriginalFilename(),
                multipartFile.getInputStream());
        WmMaterial wmMaterial=new WmMaterial();
        wmMaterial.setUserId(WmThreadLocalUtil.getUser().getId());
        wmMaterial.setUrl(url);
        wmMaterial.setType((short) 0);
        wmMaterial.setIsCollection((short) 0);
        wmMaterial.setCreatedTime(new Date());
        this.wmMaterialMapper.insert(wmMaterial);
        return ResponseResult.okResult(wmMaterial);

    }

    @Override
    public ResponseResult list(WmMaterialDto dto) {
        dto.checkParam();
        IPage page=new Page(dto.getPage(),dto.getSize());
        QueryWrapper<WmMaterial> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(WmMaterial::getUserId,WmThreadLocalUtil.getUser().getId());
        queryWrapper.orderByDesc("created_time");
        queryWrapper.lambda().eq(dto.getIsCollection()!=0,WmMaterial::getIsCollection,dto.getIsCollection());
        page=this.wmMaterialMapper.selectPage(page,queryWrapper);
        ResponseResult result=new PageResponseResult(dto.getPage(),dto.getSize(),(int)page.getTotal());
        result.setData(page.getRecords());
        return result;
    }

    @Override
    public ResponseResult collect(Integer id) {
        WmMaterial wmMaterial = new WmMaterial();
        wmMaterial.setId(id);
        wmMaterial.setIsCollection((short) 1);
        int i = this.wmMaterialMapper.updateById(wmMaterial);
        return ResponseResult.okResult(i);
    }

    //取消收藏
    @Override
    public ResponseResult cancelCollect(Integer id) {
        WmMaterial wmMaterial = new WmMaterial();
        wmMaterial.setId(id);
        wmMaterial.setIsCollection((short) 0);
        int i = this.wmMaterialMapper.updateById(wmMaterial);
        return ResponseResult.okResult(i);
    }

    @Override
    public ResponseResult delPicture(Integer id) {
        int i = this.wmMaterialMapper.deleteById(id);
        return ResponseResult.okResult(i);
    }

}
